package miaofusdk

import (
	"encoding/json"
	"errors"
	"fmt"

	"gitee.com/garlic/tools"
)

type StoreBean struct {
	Id                         int    `json:"id,omitempty,string"`
	Code                       string `json:"code,omitempty"`
	Name                       string `json:"name,omitempty"`
	AliStoreId                 string `json:"ali_store_id,omitempty"`
	UserId                     int    `json:"user_id,omitempty,string"`
	ShopId                     int    `json:"shop_id,omitempty,string"`
	OrgId                      int    `json:"org_id,omitempty,string"`
	Address                    string `json:"address,omitempty"`
	BrandName                  string `json:"brand_name,omitempty"`
	BrandLogo                  string `json:"brand_logo,omitempty"`
	ProvinceName               string `json:"province_name,omitempty"`
	ProvinceCode               string `json:"province_code,omitempty"`
	CityName                   string `json:"city_name,omitempty"`
	CityCode                   string `json:"city_code,omitempty"`
	DistrictName               string `json:"district_name,omitempty"`
	DistrictCode               string `json:"district_code,omitempty"`
	Longitude                  string `json:"longitude,omitempty"`
	Latitude                   string `json:"latitude,omitempty"`
	ContactName                string `json:"contact_name,omitempty"`
	ContactNumber              string `json:"contact_number,omitempty"`
	ContactMobile              string `json:"contact_mobile,omitempty"`
	ContactEmail               string `json:"contact_email,omitempty"`
	AvgPrice                   string `json:"avg_price,omitempty"`
	BusinessTime               string `json:"business_time,omitempty"`
	MainImage                  string `json:"main_image,omitempty"`
	ImageOutsideHeader         string `json:"image_outside_header,omitempty"`
	ImageInner1                string `json:"image_inner1,omitempty"`
	ImageInner2                string `json:"image_inner2,omitempty"`
	License                    string `json:"license,omitempty"`
	LicenceCode                string `json:"licence_code,omitempty"`
	LicenceName                string `json:"licence_name,omitempty"`
	BusinessCertificate        string `json:"business_certificate,omitempty"`
	BusinessCertificateExpires string `json:"business_certificate_expires,omitempty"`
	LicenceExpires             string `json:"licence_expires,omitempty"`
	AuthLetter                 string `json:"auth_letter,omitempty"`
	OtherAuthorization         string `json:"other_authorization,omitempty"`
	AliCategoryId              string `json:"ali_category_id,omitempty"`
	BranchName                 string `json:"branch_name,omitempty"`
	SalerId                    int    `json:"saler_id,omitempty,string"`
	MerchantName               string `json:"merchant_name,omitempty"`
	RecipientName              string `json:"recipient_name,omitempty"`
	RecipientIdCardNo          string `json:"recipient_id_card_no,omitempty"`
	RecipientWechatId          string `json:"recipient_wechat_id,omitempty"`
	WeChatCategoryId           string `json:"wechat_category_id,omitempty"`
	MicroMchId                 string `json:"micro_mch_id,omitempty"`
	AutoVerification           int    `json:"auto_verification,omitempty,string"`
	CmbcPayStatus              int    `json:"cmbc_pay_status,omitempty,string"`
	CmbcMid                    string `json:"cmbc_mid,omitempty"`
	AlipayFee                  string `json:"alipay_fee,omitempty"`
	WechatFee                  string `json:"wechat_fee,omitempty"`
	AccountNumber              string `json:"account_number,omitempty"`
	IdCard                     string `json:"id_card,omitempty"`
	HolderName                 string `json:"holder_name,omitempty"`
	IsPrivate                  string `json:"is_private,omitempty"`
	IssuerProvince             string `json:"issuer_province,omitempty"`
	IssuerCity                 string `json:"issuer_city,omitempty"`
	BankCode                   string `json:"bank_code,omitempty"`
	OtherCertImageId           string `json:"other_cert_image_id,omitempty"`
	PayChnAli                  int    `json:"pay_chn_ali,omitempty,string"`
	PayChnWx                   int    `json:"pay_chn_wx,omitempty,string"`
	MfAwordFeePoint            string `json:"mf_aword_fee_point,omitempty"`
	WxFeePoint                 string `json:"wx_fee_point,omitempty"`
	AliFeePoint                string `json:"ali_fee_point,omitempty"`
	AutoGetCard                int    `json:"auto_get_card,omitempty,string"`
	OfficialAppId              string `json:"official_app_id,omitempty"`
	OfficialAppSecret          string `json:"official_app_secret,omitempty"`
	Iso2o                      int    `json:"iso2o,omitempty,string"`
	KouBeiCode                 string `json:"koubei_code,omitempty"`
	MccCode                    string `json:"mcc_code,omitempty"`
	LicenseType                string `json:"license_type,omitempty"`
	SettlePersonID1ImageId     string `json:"settle_person_id1_image_id,omitempty"`
	SettlePersonID2ImageId     string `json:"settle_person_id2_image_id,omitempty"`
	LegalPersonID              string `json:"legal_person_id,omitempty"`
	LegalPersonName            string `json:"legal_person_name,omitempty"`
	LegalPersonPhone           string `json:"legal_person_phone,omitempty"`
	LegalPersonID1ImageId      string `json:"legal_person_id1_image_id,omitempty"`
	LegalPersonID2ImageId      string `json:"legal_person_id2_image_id,omitempty"`
	MerchantType               int    `json:"merchant_type,omitempty"`
	Data1                      string `json:"data1,omitempty"`
	Data2                      string `json:"data2,omitempty"`
	Data3                      string `json:"data3,omitempty"`
	Data4                      string `json:"data4,omitempty"`
	Data5                      string `json:"data5,omitempty"`
}

type RespStore struct {
	Response
	Data StoreBean `json:"data,omitempty"`
}

type RespStores struct {
	Response
	Page       int         `json:"page,string"`
	Rows       int         `json:"rows,string"`
	TotalCount int         `json:"total_count,string"`
	TotalPage  int         `json:"total_page,string"`
	List       []StoreBean `json:"list,omitempty,string"`
}

type RespStoreBelone struct {
	Response
	StoreId   int    `json:"store_id"`
	ShopId    int    `json:"shop_id"`
	OrgId     int    `json:"org_id"`
	StoreCode string `json:"store_code"`
	ShopCode  string `json:"shop_code"`
	OrgCode   string `json:"org_code"`
}

func (s *SrvShopManager) GetStoreBelongByCode(code string) (RespStoreBelone, error) {
	resp := RespStoreBelone{}
	http_request := s.GetPost(s.GetUrl() + "/store/getbelone")
	http_request.Param("code", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else if resp.Code != "0" {
		return resp, errors.New(resp.Msg)
	} else {
		return resp, nil
	}
}

func (s *SrvShopManager) GetStoreBelongById(store_id int) (RespStoreBelone, error) {
	resp := RespStoreBelone{}
	http_request := s.GetPost(s.GetUrl() + "/store/getbelone")
	http_request.Param("store_id", fmt.Sprintf("%d", store_id))
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else if resp.Code != "0" {
		return resp, errors.New(resp.Msg)
	} else {
		return resp, nil
	}
}

func (s *SrvShopManager) GetStoreByCode(code string) (StoreBean, error) {
	resp := RespStore{}
	http_request := s.GetPost(s.GetUrl() + "/store/query")
	http_request.Param("code", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) GetStoreById(store_id int) (StoreBean, error) {
	resp := RespStore{}
	http_request := s.GetPost(s.GetUrl() + "/store/query")
	http_request.Param("store_id", fmt.Sprintf("%d", store_id))
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) CreateStore(bean StoreBean) (StoreBean, error) {
	resp := RespStore{}
	http_request := s.GetPost(s.GetUrl() + "/store/create")
	m := tools.JsonToMap(bean)
	for k, _ := range m {
		value := fmt.Sprintf("%v", m[k])
		if value != "" {
			http_request.Param(k, value)
		}
	}
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) UpdateStore(bean StoreBean) (StoreBean, error) {
	resp := RespStore{}
	http_request := s.GetPost(s.GetUrl() + "/store/update")
	m := tools.JsonToMap(bean)
	for k, _ := range m {
		value := fmt.Sprintf("%v", m[k])
		if value != "" {
			http_request.Param(k, value)
		}
	}
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

/**
慎用...物理删除.
*/
func (s *SrvShopManager) DeleteStore(id int) error {
	resp := Response{}
	http_request := s.GetPost(s.GetUrl() + "/store/delete")
	http_request.Param("store_id", fmt.Sprintf("%d", id))
	if v, err := http_request.Bytes(); err != nil {
		return err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return err
	} else if resp.Code != "0" {
		return errors.New(resp.Msg)
	} else {
		return nil
	}
}

func (s *SrvShopManager) StoreList(page, rows, org_id, shop_id int, query_type, query_value string) (RespStores, error) {
	resp := RespStores{}
	http_request := s.GetPost(s.GetUrl() + "/store/list")
	http_request.Param("page", fmt.Sprintf("%d", page))
	http_request.Param("rows", fmt.Sprintf("%d", rows))
	http_request.Param("org_id", fmt.Sprintf("%d", org_id))
	http_request.Param("shop_id", fmt.Sprintf("%d", shop_id))
	http_request.Param("query_type", query_type)
	http_request.Param("query_value", query_value)
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else {
		return resp, nil
	}
}

type AlipayCategoryBean struct {
	Id    int    `json:"id,omitempty"`
	Code  string `json:"code,omitempty"`
	Name  string `json:"name,omitempty"`
	PAdC  string `json:"p_ad_c,omitempty"`
	Other string `json:"other,omitempty"`
	IsFan string `json:"is_fan,omitempty"`
}

type RespAlipayCategory struct {
	Response
	Data AlipayCategoryBean `json:"data,omitempty"`
}

func (s *SrvShopManager) GetAliCategoryByCode(code string) (AlipayCategoryBean, error) {
	resp := RespAlipayCategory{}
	http_request := s.GetPost(s.GetUrl() + "/category/alipay/read")
	http_request.Param("code", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

type WechatCategoryBean struct {
	Id   int    `json:"id,omitempty"`
	Code string `json:"code,omitempty"`
	Name string `json:"name,omitempty"`
	PAdC string `json:"p_ad_c,omitempty"`
}

type RespWechatCategory struct {
	Response
	Data WechatCategoryBean `json:"data,omitempty"`
}

func (s *SrvShopManager) GetWxCategoryByCode(code string) (WechatCategoryBean, error) {
	resp := RespWechatCategory{}
	http_request := s.GetPost(s.GetUrl() + "/category/wechat/read")
	http_request.Param("code", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

type BankCodeBean struct {
	Id           int    `json:"id,omitempty"`
	BankBranchNo string `json:"bank_branch_no,omitempty"`
	BankName     string `json:"bank_name,omitempty"`
	BankBranch   string `json:"bank_branch,omitempty"`
	BankNo       string `json:"bank_no,omitempty"`
	BankNameAddr string `json:"bank_name_addr,omitempty"` //简称
}

type RespBankCodeBean struct {
	Response
	Data BankCodeBean `json:"data,omitempty"`
}

func (s *SrvShopManager) GetBankCode(code string) (BankCodeBean, error) {
	resp := RespBankCodeBean{}
	http_request := s.GetPost(s.GetUrl() + "/bankcode/read")
	http_request.Param("bank_branch_no", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

type CityCodeBean struct {
	Id     int    `json:"id,omitempty"`
	AdCode string `json:"ad_code,omitempty"`
	Name   string `json:"name,omitempty"`
	PAdC   string `json:"p_ad_c,omitempty"`
}

type RespCityCodeBean struct {
	Response
	Data CityCodeBean `json:"data,omitempty"`
}

func (s *SrvShopManager) GetCityByCode(code string) (CityCodeBean, error) {
	resp := RespCityCodeBean{}
	http_request := s.GetPost(s.GetUrl() + "/citycode/read")
	http_request.Param("ad_code", code)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}

func (s *SrvShopManager) GetCityByName(name string) (CityCodeBean, error) {
	resp := RespCityCodeBean{}
	http_request := s.GetPost(s.GetUrl() + "/citycode/readbyname")
	http_request.Param("name", name)
	if v, err := http_request.Bytes(); err != nil {
		return resp.Data, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp.Data, err
	} else if resp.Code != "0" {
		return resp.Data, errors.New(resp.Msg)
	} else {
		return resp.Data, nil
	}
}
